package net.zjitc.android2019.ch5broadcast;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import net.zjitc.android2019.R;

/**
 * 界面（广播发送者）：发送广播
 * 注册广播
 */
public class Ch5HelpActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn;
    MyReceiver2 myReceiver2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch5_help);
        btn=findViewById(R.id.ch5_help_btn);
        btn.setOnClickListener(this);
        myReceiver2=new MyReceiver2();
    }

    /* 动态注册广播*/
    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter=new IntentFilter();
        filter.addAction("net.zjitc.help");
        registerReceiver(myReceiver2,filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(myReceiver2);
    }

    @Override
    public void onClick(View v) {
        /* 按钮点击事件：发送广播*/
        Intent intent=new Intent();
        intent.setAction("net.zjitc.help");
        sendBroadcast(intent);
    }
}
